package com.adamfass.tractorbeam;

import com.jme.input.KeyInput;
import com.jme.input.KeyInputListener;
import com.jme.math.Vector3f;

public class KeyboardFlyingSaucerController 
    implements Controller {
  private final int leftKey;
  private final int rightKey;
  private final int downKey;
  private final int upKey;
  private final int beamKey;
  private final int disruptorKey;
  
  private boolean disruptorPodWasFired = false;
  
  public KeyboardFlyingSaucerController(
      int leftKey, int rightKey, int downKey, int upKey, int beamKey, int disruptorKey) {
    this.leftKey = leftKey;
    this.rightKey = rightKey;
    this.downKey = downKey;
    this.upKey = upKey;
    this.beamKey = beamKey;
    this.disruptorKey = disruptorKey;
    
    KeyInput.get().addListener(new KeyInputListener() {
      public void onKey(char character, int keyCode, boolean pressed) {
        if (keyCode == KeyboardFlyingSaucerController.this.disruptorKey
            && pressed) {
          disruptorPodWasFired = true;
        }
      }
    });
  }
  
  public Vector3f getThrustVector() {
    float x = 0;
    if (KeyInput.get().isKeyDown(leftKey)) {
      x -= 1.0;
    }
    if (KeyInput.get().isKeyDown(rightKey)) {
      x += 1.0;
    }
    
    float y = 0;
    if (KeyInput.get().isKeyDown(downKey)) {
      y -= 1.0;
    }
    if (KeyInput.get().isKeyDown(upKey)) {
      y += 1.0;
    }
    
    return new Vector3f(x, y, 0).normalize();
  }
  
  public boolean isTractorBeamOn() {
    return KeyInput.get().isKeyDown(beamKey);
  }
  
  public boolean disruptorPodWasFired() {
    return disruptorPodWasFired;
  }
  
  public void clearEventState() {
    disruptorPodWasFired = false;
  }
}
